package lib

import (
	"algorithms/ds"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         21,
		Discription: "合并两个升序链表",
		Level:       1,
		Labels: map[string]int{
			"链表": 1,
		},
	})
}

func MergeTwoLists(list1 *ds.ListNode, list2 *ds.ListNode) *ds.ListNode {
	head := &ds.ListNode{}
	temp := head

	for list1 != nil || list2 != nil {
		if list1 == nil {
			temp.Next = list2
			list2 = list2.Next
			temp = temp.Next
			continue
		}

		if list2 == nil {
			temp.Next = list1
			list1 = list1.Next
			temp = temp.Next
			continue
		}

		if list1.Val < list2.Val {
			temp.Next = list1
			list1 = list1.Next
		} else {
			temp.Next = list2
			list2 = list2.Next
		}
		temp = temp.Next
	}

	return head.Next
}
